package controller

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"k8s-platform/controller/cicd_projects"
	"k8s-platform/controller/cmdb"
	"k8s-platform/controller/cmdb/xshell"
	"k8s-platform/controller/jenkins"
	"k8s-platform/controller/k8s-ui"
	"k8s-platform/controller/put_file"
	"k8s-platform/controller/release_system"
	"k8s-platform/controller/tools"
	"net/http"
)

var Router router

type router struct{}

// InitApiRouter 初始化所有API路由
func (*router) InitApiRouter(r *gin.Engine, db *gorm.DB) {
	// 初始化处理器实例
	//k8sDeployHandler := release_system.K8sDeployHandler(db)

	// 测试接口
	r.GET("/testapi", func(ctx *gin.Context) {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "testapi success!",
			"data": nil,
		})
	})

	// 登录接口
	r.POST("/api/login", k8s_ui.Login.Auth)
	// K8s部署相关接口
	install_k8s := r.Group("/api/k8s")
	{
		install_k8s.GET("/k8s_instaall_list", release_system.K8sDeployHandler.GetInstallK8s)
		install_k8s.POST("/create_deploy_config", release_system.K8sDeployHandler.Create_Deploy_Config)
		install_k8s.POST("/ansible_init", release_system.K8sDeployHandler.Ansible_init)
		install_k8s.POST("/GenerateAutomationScript_Install", release_system.K8sDeployHandler.GenerateAutomationScript_Install)
		install_k8s.PUT("/update_deploy_config", release_system.K8sDeployHandler.Update_Deploy_Config)
		install_k8s.DELETE("/delete_deploy_config", release_system.K8sDeployHandler.Delete_Deploy_Config)
	}

	// K8s核心资源相关接口
	k8sGroup := r.Group("/api/k8s")
	{
		// Pod操作
		k8sGroup.GET("/pods", k8s_ui.Pod.GetPods)
		k8sGroup.GET("/pod/detail", k8s_ui.Pod.GetPodDetail)
		k8sGroup.DELETE("/pod/del", k8s_ui.Pod.DeletePod)
		k8sGroup.PUT("/pod/update", k8s_ui.Pod.UpdatePod)
		k8sGroup.GET("/pod/container", k8s_ui.Pod.GetPodContainer)
		k8sGroup.GET("/pod/log", k8s_ui.Pod.GetContainerLog)
		k8sGroup.GET("/pod/numns", k8s_ui.Pod.GetPodNumPerNs)

		// Deployment操作
		k8sGroup.POST("/deployment/create", k8s_ui.Deployment.CreateDeployment)
		k8sGroup.POST("/deployment/create_test", k8s_ui.Deployment.CreateDeployment_test)
		k8sGroup.GET("/deployments", k8s_ui.Deployment.GetDeployments)
		k8sGroup.GET("/deployment/detail", k8s_ui.Deployment.GetDeploymentDetail)
		k8sGroup.PUT("/deployment/scale", k8s_ui.Deployment.ScaleDeployment)
		k8sGroup.DELETE("/deployment/del", k8s_ui.Deployment.DeleteDeployment)
		k8sGroup.PUT("/deployment/restart", k8s_ui.Deployment.RestartDeployment)
		k8sGroup.PUT("/deployment/update", k8s_ui.Deployment.UpdateDeployment)
		k8sGroup.GET("/deployment/numns", k8s_ui.Deployment.GetDeployNumPerNp)

		// DaemonSet操作
		k8sGroup.GET("/daemonsets", k8s_ui.DaemonSet.GetdaemonSets)
		k8sGroup.GET("/daemonset/detail", k8s_ui.DaemonSet.GetDaemonSetDetail)
		k8sGroup.POST("/daemonset/create", k8s_ui.DaemonSet.CreateDaemonSet)
		k8sGroup.PUT("/daemonset/restart", k8s_ui.DaemonSet.RestartDaemonSet)
		k8sGroup.PUT("/daemonset/update", k8s_ui.DaemonSet.UpdateDaemonSet)
		k8sGroup.DELETE("/daemonset/del", k8s_ui.DaemonSet.DeleteDaemonSett)
		k8sGroup.GET("/daemonset/numnp", k8s_ui.DaemonSet.GetdaemonSetNumPerNp)

		// StatefulSet操作
		k8sGroup.GET("/statefulset", k8s_ui.StatefulSet.GetStatefulSets)
		k8sGroup.GET("/statefulset/detail", k8s_ui.StatefulSet.GetstatefulsetDetail)
		k8sGroup.POST("/statefulset/create", k8s_ui.StatefulSet.CreateStatefulSet)
		k8sGroup.PUT("/statefulset/restart", k8s_ui.StatefulSet.RestartStatefulSet)
		k8sGroup.DELETE("/statefulset/del", k8s_ui.StatefulSet.DeleteStatefulSet)
		k8sGroup.PUT("/statefulset/scale", k8s_ui.StatefulSet.ScaleStatefulSet)
		k8sGroup.PUT("/statefulset/update", k8s_ui.StatefulSet.UpdateStatefulSet)
		k8sGroup.GET("/statefulset/numnp", k8s_ui.StatefulSet.GetStatefulSetNumPerNp)

		// Node操作
		k8sGroup.GET("/nodes", k8s_ui.Node.GetNodes)
		k8sGroup.GET("/node/detail", k8s_ui.Node.GetNodeDetail)

		// Namespace操作
		k8sGroup.POST("/namespaces/create", k8s_ui.Namespace.CreateNamespace)
		k8sGroup.GET("/namespaces", k8s_ui.Namespace.GetNameSpaces)
		k8sGroup.GET("/namespace/detail", k8s_ui.Namespace.GetNameSpaceDetail)
		k8sGroup.DELETE("/namespace/del", k8s_ui.Namespace.DeleteNamespace)

		// 存储相关
		k8sGroup.GET("/persistentvolume", k8s_ui.PersistentVolume.GetPersistentVolume)
		k8sGroup.GET("/persistentvolume/detail", k8s_ui.PersistentVolume.GetPersistentVolumeDetail)
		k8sGroup.DELETE("/persistentvolume/del", k8s_ui.PersistentVolume.DeletePersistentVolume)
		k8sGroup.GET("/persistentvolumeclaims", k8s_ui.PersistentVolumeClaim.GetPersistentVolumeClaim)
		k8sGroup.POST("/persistentvolumeclaim/create", k8s_ui.PersistentVolumeClaim.CreatePersistentVolumeClaim)
		k8sGroup.GET("/persistentvolumeclaim/detail", k8s_ui.PersistentVolumeClaim.GetPersistentVolumeClaimDetail)
		k8sGroup.DELETE("/persistentvolumeclaim/del", k8s_ui.PersistentVolumeClaim.DeletePersistentVolumeClaim)
		k8sGroup.GET("/storageclasses", k8s_ui.StorageClasses.GetStorageClasses)

		// 网络相关
		k8sGroup.GET("/services", k8s_ui.K8s_service.GetServices)
		k8sGroup.GET("/service/detail", k8s_ui.K8s_service.GetServiceDetail)
		k8sGroup.POST("/service/create", k8s_ui.K8s_service.CreateService)
		k8sGroup.DELETE("/service/del", k8s_ui.K8s_service.DeleteService)
		k8sGroup.PUT("/service/update", k8s_ui.K8s_service.UpdateService)
		k8sGroup.GET("/service/numnp", k8s_ui.K8s_service.GetServiceNumPerNp)
		k8sGroup.GET("/ingresses", k8s_ui.Ingress.GetIngress)
		k8sGroup.GET("/ingresses/detail", k8s_ui.Ingress.GetIngressDetail)
		k8sGroup.POST("/ingresses/create", k8s_ui.Ingress.CreateIngress)
		k8sGroup.DELETE("/ingresses/del", k8s_ui.Ingress.DeleteIngress)
		k8sGroup.PUT("/ingresses/update", k8s_ui.Ingress.UpdateIngress)
		k8sGroup.GET("/ingressclass", k8s_ui.Ingress.GetIngressClassName)

		// 配置相关
		k8sGroup.GET("/configmaps", k8s_ui.K8s_ConfigMap.GetConfigMap)
		k8sGroup.GET("/configmap/detail", k8s_ui.K8s_ConfigMap.GetConfigMapDetail)
		k8sGroup.POST("/configmap/create", k8s_ui.K8s_ConfigMap.CreateConfigMap)
		k8sGroup.DELETE("/configmap/del", k8s_ui.K8s_ConfigMap.DeleteConfigMap)
		k8sGroup.PUT("/configmap/update", k8s_ui.K8s_ConfigMap.UpdateConfigMap)
		k8sGroup.GET("/configmap/numnp", k8s_ui.K8s_ConfigMap.GetConfigMapNumPerNp)
		k8sGroup.GET("/secrets", k8s_ui.Secret.GetSecrets)
		k8sGroup.GET("/secret/detail", k8s_ui.Secret.GetSecretDetail)
		k8sGroup.POST("/secret/create", k8s_ui.Secret.CreateSecret)
		k8sGroup.DELETE("/secret/del", k8s_ui.Secret.DeleteSecret)
		k8sGroup.PUT("/secret/update", k8s_ui.Secret.UpdateSecret)
		k8sGroup.GET("/secret/numnp", k8s_ui.Secret.GetSecretNumPerNp)

		// 工作流和集群管理
		k8sGroup.GET("/workflows", k8s_ui.Workflow.GetList)
		k8sGroup.GET("/workflow/detail", k8s_ui.Workflow.GetById)
		k8sGroup.POST("/workflow/create", k8s_ui.Workflow.Create)
		k8sGroup.DELETE("/workflow/del", k8s_ui.Workflow.DelById)
		k8sGroup.POST("/createK8s/create", k8s_ui.K8s_create.CreateK8s)
		k8sGroup.DELETE("/createK8s/del", k8s_ui.K8s_create.DelK8s)
		k8sGroup.GET("/createK8s/intpool", k8s_ui.K8s_create.IntPool)
		k8sGroup.GET("/createK8s/list", k8s_ui.K8s_create.GetList)
	}

	// CMDB相关接口
	cmdbGroup := r.Group("/api/cmdb")
	{
		cmdbGroup.POST("/servergroup/add", cmdb.HostGroup.Add)
		cmdbGroup.GET("/servergroup/list", cmdb.HostGroup.List)
		cmdbGroup.GET("/servergroup/detail", cmdb.HostGroup.GetDetail)
		cmdbGroup.PUT("/servergroup/update", cmdb.HostGroup.Update)
		cmdbGroup.DELETE("/servergroup/delete", cmdb.HostGroup.Delete)
		cmdbGroup.POST("/ssh/createserver", cmdb.Hostssh.Createssh)
		cmdbGroup.GET("/server/list", cmdb.HostServer.List)
		cmdbGroup.GET("/server/detail", cmdb.HostServer.GetDetail)
		cmdbGroup.PUT("/server/update", cmdb.HostServer.Update)
		cmdbGroup.POST("/ssh/sync", cmdb.Hostssh.Sync)
		cmdbGroup.DELETE("/server/delete", cmdb.HostServer.Delete)
		cmdbGroup.POST("/credential/add", cmdb.Credential.Add)
		cmdbGroup.GET("/credential/list", cmdb.Credential.List)
		cmdbGroup.GET("/credential/detail", cmdb.Credential.GetDetail)
		cmdbGroup.PUT("/credential/update", cmdb.Credential.Update)
		cmdbGroup.DELETE("/credential/delete", cmdb.Credential.Delete)
	}

	// GitLab相关接口
	gitGroup := r.Group("/api/git")
	{
		gitGroup.GET("/list", release_system.Gitlab.List)
		gitGroup.POST("/branchList", release_system.Gitlab.BrancheList)
	}

	// Jenkins相关接口
	jenkinsGroup := r.Group("/api/jenkins")
	{
		jenkinsGroup.POST("/createjob", jenkins.Jenkinsjob.Create)
		jenkinsGroup.POST("/runjob", jenkins.Jenkins.RunJob)
		jenkinsGroup.POST("/joblist", jenkins.Jenkins.Joblist)
		jenkinsGroup.POST("/stopjob", jenkins.Jenkins.StouJob)
		jenkinsGroup.GET("/jobs", jenkins.Jenkins.GetJobs)
		jenkinsGroup.GET("/getconfig", jenkins.Jenkins.Getjobconfig)
		jenkinsGroup.PUT("/updateconfig", jenkins.Jenkins.UpdateConfig)
		jenkinsGroup.DELETE("/deletejob", jenkins.Jenkins.Deletejob)
		jenkinsGroup.GET("/nodes", jenkins.Jenkins.GetNodes)
		jenkinsGroup.GET("/views", jenkins.Jenkins.GetViewNames)
		jenkinsGroup.GET("/yamllist", jenkins.Jenkins.GetYAML)
	}

	// Harbor相关接口
	harborGroup := r.Group("/api/harbor")
	{
		harborGroup.GET("/projects", tools.Harbor_data.Harbor_projects)
		harborGroup.GET("/prepositories", tools.Harbor_data.Harbor_prepositories)
		harborGroup.GET("/images/version", tools.Harbor_data.Harbor_images_list)
	}

	// CICD相关接口
	cicdGroup := r.Group("/api/cicd")
	{
		cicdGroup.POST("/projects", cicd_projects.Cicdprojects.Create_apps)
		cicdGroup.GET("/projectslist", cicd_projects.Cicdprojects.List)
		cicdGroup.POST("/runapps", cicd_projects.Cicdprojects.Run_apps)
		cicdGroup.POST("/deleteapps", cicd_projects.Cicdprojects.Delete_apps)
	}

	// 其他功能接口
	r.POST("/api/upload/avatar", put_file.PUT_File.PUT_avatar)
	r.GET("/api/node/ssh", xshell.WsSsh)
}
